<?php


namespace app\admin\controller\mall;


use app\common\model\MallMember;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * Class Member
 * @package app\admin\controller\mall
 * @ControllerAnnotation(title="会员管理")
 */
class Member extends AdminController
{

    use Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallMember();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach($list as $k=>$v){
                if(!$v['avatarUrl']){
                    $list[$k]['avatarUrl'] ='https://'.$_SERVER['HTTP_HOST'].'/static/icon3.png';
                }
                if($v['usertype']>0){
                    $cardtype =(new \app\common\business\Membercard())->getMembercardInfo($v['usertype']);
                    $list[$k]['title'] =$cardtype['title'];
                }else{
                    $list[$k]['title'] ='--';
                }

            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            // 增加数目
            if($post['balancetype'] ==1){
                if($post['changenum']<=0){
                    $this->error('改变数目必须大于0');
                }
                (new \app\common\business\Member())->saveAccoutLog($row['openid'],$post['changenum'],4,'平台增加');
            }else{
                if(round($row['balance']-$post['changenum'],2)<0){
                    $this->error('改变数目不能大于目前余额');
                }
                (new \app\common\business\Member())->saveAccoutLog($row['openid'],$post['changenum'],0,'平台减少');
            }

            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }


}